{- This file is part of funbot.
 -
 - Written in 2015, 2016, 2017 by fr33domlover <fr33domlover@riseup.net>.
 -
 - ♡ Copying is an act of love. Please copy, reuse and share.
 -
 - The author(s) have dedicated all copyright and related and neighboring
 - rights to this software to the public domain worldwide. This software is
 - distributed without any warranty.
 -
 - You should have received a copy of the CC0 Public Domain Dedication along
 - with this software. If not, see
 - <http://creativecommons.org/publicdomain/zero/1.0/>.
 -}

{-# LANGUAGE OverloadedStrings #-}

module FunBot.Commands.Misc
    ( cmdEcho
    , cmdQuote
    , cmdFolks
    )
where

import Control.Monad (unless, when)
import Data.List (find, intercalate)
import Data.Monoid ((<>))
import Data.Settings.Types (showOption)
import Data.Text (Text)
import Database.Persist
import Network.IRC.Fun.Bot.Behavior
import Network.IRC.Fun.Bot.Chat
import Network.IRC.Fun.Bot.State
import Network.IRC.Fun.Bot.Types
import Text.Read (readMaybe)
import Network.IRC.Fun.Types.Base

import qualified Data.CaseInsensitive as CI
import qualified Data.Text as T
import qualified Data.Text.Read as TR

import FunBot.History (quote, reportHistory')
import FunBot.Memos (submitMemo)
import FunBot.Model
import FunBot.Settings
import FunBot.Settings.Sections.Channels (addChannel)
import FunBot.Settings.Sections.Feeds (addFeed, deleteFeed)
import FunBot.Settings.Sections.Repos
import FunBot.Settings.Sections.Shortcuts (addShortcut, deleteShortcut)
import FunBot.Types
import FunBot.UserOptions
import FunBot.Util (getHistoryLines, cmds, helps, runDB)

-------------------------------------------------------------------------------
-- Echo command
-- Send the input back to the IRC channel
-------------------------------------------------------------------------------

respondEcho
    :: Maybe Channel
    -> Nickname
    -> [Text]
    -> (MsgContent -> BotSession ())
    -> BotSession ()
respondEcho _mchan _nick []      send = send $ MsgContent " "
respondEcho _mchan _nick [param] send = send $ MsgContent param
respondEcho _mchan _nick params  send = send $ MsgContent $ T.unwords params

cmdEcho = Command
    { cmdNames    = cmds ["echo"]
    , cmdRespond  = respondEcho
    , cmdHelp     = helps
        [ ( "echo <text>"
          , "display the given text. Probably not a useful command. Exists as \
            \an example and for testing."
          )
        ]
    , cmdExamples =
        [ "echo This is a test"
        ]
    }

-------------------------------------------------------------------------------
-- Quote command
-- Record something someone said
-------------------------------------------------------------------------------

respondQuote
    :: Maybe Channel
    -> Nickname
    -> [Text]
    -> (MsgContent -> BotSession ())
    -> BotSession ()
respondQuote (Just chan) _nick [target] _send = quote chan $ Nickname target
respondQuote Nothing     _nick [_]      _send = return ()
respondQuote mchan       nick  args     _send =
    failBack mchan nick $ WrongNumArgsN (Just $ length args) (Just 1)

cmdQuote = Command
    { cmdNames    = cmds ["quote"]
    , cmdRespond  = respondQuote
    , cmdHelp     = helps
        [ ( "quote <nick>"
          , "record the last message sent by <nick> in the channel. The list \
            \of past quotes is publicly available."
          )
        ]
    , cmdExamples =
        [ "quote johndoe"
        ]
    }

-------------------------------------------------------------------------------
-- Folks command
-- Ping some channel representatives
-------------------------------------------------------------------------------

respondFolks
    :: Maybe Channel
    -> Nickname
    -> [Text]
    -> (MsgContent -> BotSession ())
    -> BotSession ()
respondFolks Nothing     _nick _    send  = send "Works only in a channel"
respondFolks chan        nick  []   send  = respondFolks chan nick ["^"] send
respondFolks (Just chan) nick  msg  send  = do
    folks <- runDB $ selectList [FolkChannel ==. chan] []
    if null folks
        then send "No folks to ping"
        else send $ MsgContent $
            T.intercalate ", "
                (map (unNickname . folkNickname . entityVal) folks) <>
            T.unwords (":":msg)

cmdFolks = Command
    { cmdNames    = cmds ["folks"]
    , cmdRespond  = respondFolks
    , cmdHelp     = helps
        [ ( "folks"
          , "ping the nice channel representatives"
          )
        ]
    , cmdExamples =
        [ "folks"
        ]
    }
